package com.holyn.mylibrary.test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Holyn on 2017/6/9.
 */

public class test {
    public static void main(String[] args) {
        isNumberWord();
    }

    private static void testComplie(){
        Pattern pattern = Pattern.compile("\\w+");
        System.out.println(pattern.pattern());
        System.out.println(pattern.matcher("abc").matches());
    }

    private static void testSplit(){
        Pattern p=Pattern.compile("\\d+");
        String[] str=p.split("我的QQ是:456456我的电话是:0532214我的邮箱是:aaa@aaa.com");
        for (String s : str){
            System.out.println(s);
        }
    }

    private static void testMatcher(){
        Pattern.matches("\\d+","2223");//返回true
        Pattern.matches("\\d+","2223aa");//返回false,需要匹配到所有字符串才能返回true,这里aa不能匹配到
        Pattern.matches("\\d+","22bb23");//返回false,需要匹配到所有字符串才能返回true,这里bb不能匹配到
    }

    private static void testMatcher2(){
//        4.Matcher.matches()/ Matcher.lookingAt()/ Matcher.find()
//        Matcher类提供三个匹配操作方法,三个方法均返回boolean类型,当匹配到时返回true,没匹配到则返回false

//        matches()对整个字符串进行匹配,只有整个字符串都匹配了才返回true

        Pattern p=Pattern.compile("\\d+");
        Matcher m=p.matcher("22bb23");
        m.matches();//返回false,因为bb不能被\d+匹配,导致整个字符串匹配未成功.
        Matcher m2=p.matcher("2223");
        m2.matches();//返回true,因为\d+匹配到了整个字符串

//        我们现在回头看一下Pattern.matcher(String regex,CharSequence input),它与下面这段代码等价
//        Pattern.compile(regex).matcher(input).matches()
    }

    private static void testMatcher3(){
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher("2223");
        matcher.matches();
        matcher.find();
        matcher.lookingAt();

        //lookingAt()对前面的字符串进行匹配,只有匹配到的字符串在最前面才返回true
        Pattern p=Pattern.compile("\\d+");
        Matcher m=p.matcher("22bb23");
        m.lookingAt();//返回true,因为\d+匹配到了前面的22
        Matcher m2=p.matcher("aa2223");
        m2.lookingAt();//返回false,因为\d+不能匹配前面的aa

    }

    private static void testMatcher4(){
        //find()对字符串进行匹配,匹配到的字符串可以在任何位置.
        Pattern p=Pattern.compile("\\d+");
        Matcher m=p.matcher("22bb23");
        m.find();//返回true
        Matcher m2=p.matcher("aa2223");
        m2.find();//返回true
        Matcher m3=p.matcher("aa2223bb");
        m3.find();//返回true
        Matcher m4=p.matcher("aabb");
        m4.find();//返回false
    }

    private static void testMatcher5() {
        Pattern p = Pattern.compile("\\d+");
        Matcher m = p.matcher("aaa2223bb");
        m.find();//匹配2223
        m.start();//返回3
        m.end();//返回7,返回的是2223后的索引号
        m.group();//返回2223
    }

    private static void testMatcherGROUP() {
        Pattern p=Pattern.compile("([a-z]+)(\\d+)");
        Matcher m=p.matcher("aaa2223bb");
        m.find();   //匹配aaa2223
        m.groupCount();   //返回2,因为有2组
        m.start(1);   //返回0 返回第一组匹配到的子字符串在字符串中的索引号
        m.start(2);   //返回3
        m.end(1);   //返回3 返回第一组匹配到的子字符串的最后一个字符在字符串中的索引位置.
        m.end(2);   //返回7
        m.group(1);   //返回aaa,返回第一组匹配到的子字符串
        m.group(2);   //返回2223,返回第二组匹配到的子字符串
    }

    private static void testMatcher6() {
        Pattern p=Pattern.compile("\\d+");
        Matcher m=p.matcher("我的QQ是:456456 我的电话是:0532214 我的邮箱是:aaa123@aaa.com");
        while(m.find()) {
            System.out.println(m.group());
        }

        while(m.find()) {
            System.out.println(m.group());
            System.out.print("start:"+m.start());
            System.out.println(" end:"+m.end());
        }
    }

    private static void check(){
        Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]");
        System.out.println(pattern.matcher("123好啊").find());
        System.out.println(pattern.matcher("123bb").find());
    }

    private static void isNumberWord(){

        String regex = "^(\\w)+(\\.\\w+)*@(\\w)+((\\.\\w{2,3}){1,3})$";
        Pattern pattern = Pattern.compile(regex);
        System.out.println(pattern.matcher("13590604841").matches());
        System.out.println(pattern.matcher("13590604").matches());
        System.out.println(pattern.matcher("135906044444444444").matches());
        System.out.println(pattern.matcher("22452@qq.com").matches());
        System.out.println(pattern.matcher("22452@163.com").matches());
        System.out.println(pattern.matcher("22452@qq").matches());
    }



}
